package com.example.warehouse.service.serviceImpl;


import com.example.warehouse.dto.AssignRoleDto;
import com.example.warehouse.entity.User;
import com.example.warehouse.entity.userRole;
import com.example.warehouse.mapper.RoleMapper;
import com.example.warehouse.mapper.RoleUserMapper;
import com.example.warehouse.mapper.userMapper;
import com.example.warehouse.page.Page1;
import com.example.warehouse.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class userServiceImpl  implements userService{

   @Autowired
   private userMapper userMapper;


    @Override
    public User LoginUser(String usercode) {
        return userMapper.findUserByCode(usercode);
    }

    @Override
    public Page1 PageQuery(Page1 page1, User user) {
        int count = userMapper.findUserCount(user);
        List<User> userList = userMapper.FindUserByPage(page1, user);
        page1.setTotalNum(count);
        page1.setResultList(userList);
        return page1;
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public boolean upUserState(User user) {
        userMapper.updataSatae(user.getUserId(),user.getUserState());
        return true;
    }

    @Override
    public boolean deleteById(Integer userId) {
        userMapper.deleteById(userId);
        return true;
    }

    @Override
    public boolean updateUserById(String userName, int userId, Date updateTime) {
        userMapper.updateUserById(userName,userId,updateTime);
        return true;
    }

    @Override
    public boolean updateUserPwd(Integer userId, String originPwd) {
        userMapper.updateUserPwd(userId,originPwd);
        return true;
    }

    @Autowired
    private RoleUserMapper roleUserMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void assignRoleDtoDemo(AssignRoleDto assignRoleDto) {
        Integer i = roleUserMapper.removeRoleByUid(assignRoleDto.getUserId());
        if(i>1){
            List<String> roleCheckList = assignRoleDto.getRoleCheckList();
            for(String rolename:roleCheckList){
                Integer roleId = roleMapper.findUserId(rolename);
                userRole userRole=new userRole();
                userRole.setRoleId(roleId);
                userRole.setUserRoleId(assignRoleDto.getUserId());
                roleUserMapper.AddUserRole(userRole);
            }
        }

    }

    @Override
    public List<Integer> findUserAuth(Integer userId) {
        return userMapper.FindUserAuth(userId);
    }



}
